package com.gdut.simple.view.activity.user;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.model.IUpdateInfoModel;
import com.gdut.simple.model.impl.UpdateBirthdayModelImpl;
import com.gdut.simple.model.impl.UpdateGenderModelImpl;
import com.gdut.simple.view.basic.AbsBasicActivity;
import com.google.gson.JsonObject;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.functions.Action1;

public class UserPersonalInfoActivity extends AbsBasicActivity {

    private static final String TAG = UserPersonalInfoActivity.class.getSimpleName();

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_post_author)
    TextView mUsername;
    @BindView(R.id.id_follow_user_profile)
    CircleImageView mUserProfile;
    @BindView(R.id.id_user_nickname)
    TextView mUserNickname;
    @BindView(R.id.id_user_gender)
    TextView mUserGender;
    @BindView(R.id.id_user_birthday)
    TextView mUserBirthday;
    @BindView(R.id.id_user_phone)
    TextView mUserPhone;
    @BindView(R.id.id_user_email)
    TextView mUserEmail;
    @BindView(R.id.id_user_area)
    TextView mUserArea;
    @BindView(R.id.id_user_intro)
    TextView mUserIntro;
    @BindView(R.id.id_user_site)
    TextView mUserSite;

    @OnClick(R.id.id_real_name_auth)
    void gotoAuth() {

    }

    @OnClick(R.id.id_user_qr_code)
    void gotoQr() {
        String data = BmobUser.getCurrentUser(User.class).getObjectId();
        QRCodeActivity.startActivity(this, BmobUser.getCurrentUser(User.class), data);
    }

    @OnClick(R.id.id_user_profile_layout)
    void gotoUpdateProfile() {
        startActivity(new Intent(UserPersonalInfoActivity.this, UpdateProfileActivity.class));
    }

    @OnClick(R.id.id_user_nickname_layout)
    void gotoUpdateNickName() {
        startActivity(new Intent(UserPersonalInfoActivity.this, UpdateNameActivity.class));
    }

    @OnClick(R.id.id_user_gender_layout)
    void gotoUpdateGender() {
        final User currentUser = BmobUser.getCurrentUser(User.class);
        if (currentUser == null || currentUser.getObjectId() == null) {
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("性别");
        final String[] gender = {"男", "女"};
        builder.setItems(gender, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                IUpdateInfoModel updateInfoModel = new UpdateGenderModelImpl();
                updateInfoModel.update(BmobUser.getCurrentUser(User.class), gender[i], new RequestCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        RxBus.getDefault().post(new UserEvent(true));
                        ToastUtils.showShort(UserPersonalInfoActivity.this, "修改成功");
                    }

                    @Override
                    public void onFailure(String message) {
                        ToastUtils.showShort(UserPersonalInfoActivity.this, "error");
                    }
                });
            }
        });
        builder.show();
    }

    @OnClick(R.id.id_user_birthday_layout)
    void gotoUpdateBirthday() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        final DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                String birthday = year + "-" + (month + 1) + "-" + day;
                IUpdateInfoModel updateInfoModel = new UpdateBirthdayModelImpl();
                updateInfoModel.update(BmobUser.getCurrentUser(User.class), birthday, new RequestCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        RxBus.getDefault().post(new UserEvent(true));
                        ToastUtils.showShort(UserPersonalInfoActivity.this, "修改成功");
                    }

                    @Override
                    public void onFailure(String message) {
                        ToastUtils.showShort(UserPersonalInfoActivity.this, "error");
                    }
                });
            }
        }, year, month, day);
        dialog.show();
    }

    @OnClick(R.id.id_user_area_layout)
    void gotoArea() {
        startActivity(new Intent(UserPersonalInfoActivity.this, ProvinceActivity.class));
    }

    @OnClick(R.id.id_user_intro_layout)
    void gotoUpdateIntro() {
        startActivity(new Intent(UserPersonalInfoActivity.this, UpdateIntroActivity.class));
    }

    @OnClick(R.id.id_user_site_layout)
    void gotoUpdateSite() {
        startActivity(new Intent(UserPersonalInfoActivity.this, UpdateSiteActivity.class));
    }

    @OnClick(R.id.id_user_phone_layout)
    void gotoUpdateUserPhone() {

    }

    @OnClick(R.id.id_user_email_layout)
    void gotoUpdateUserEmail() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_user_personal_info);
        ButterKnife.bind(this);
        initToolbar();
    }

    private void initToolbar() {
        mToolbar.setTitle(R.string.user_center);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        loadInfo();
        mSubscription = RxBus.getDefault().toObservable(UserEvent.class).subscribe(new Action1<UserEvent>() {
            @Override
            public void call(UserEvent event) {
                if (event.isOnline()) {
                    loadInfo();
                }
            }
        });
    }

    private void loadInfo() {
        if (isLogin()) {
            User user = BmobUser.getCurrentUser(User.class);
            if (user.getUsername() == null || user.getUsername().isEmpty()) {
                mUsername.setText("********");
            } else {
                mUsername.setText(user.getUsername());
            }
            if (user.getProfile() != null) {
                Picasso.with(this)
                        .load(user.getProfile().getUrl())
                        .error(R.drawable.ic_load_error)
                        .into(mUserProfile);
            } else {
                Picasso.with(this)
                        .load(R.drawable.ic_load_error)
                        .into(mUserProfile);
            }
            if (user.getNickName() == null || user.getNickName().isEmpty()) {
                mUserNickname.setText("无名无姓");
            } else {
                mUserNickname.setText(user.getNickName());
            }
            if (user.getGender() == null || user.getGender().isEmpty()) {
                mUserGender.setText("保密");
            } else {
                mUserGender.setText(user.getGender());
            }
            if (user.getBirthday() == null || user.getBirthday().isEmpty()) {
                mUserBirthday.setText("1990-1-1");
            } else {
                mUserBirthday.setText(user.getBirthday());
            }
            if (user.getArea() == null || user.getArea().isEmpty()) {
                mUserArea.setText("无人区");
            } else {
                mUserArea.setText(user.getArea());
            }
            if (user.getIntro() == null || user.getIntro().isEmpty()) {
                mUserIntro.setText("此人很懒，什么都不说=.=!!");
            } else {
                mUserIntro.setText(user.getIntro());
            }
            if (user.getSite() == null || user.getSite().isEmpty()) {
                mUserSite.setText("暂时木有主页~~");
            } else {
                mUserSite.setText(user.getSite());
            }
            if (user.getMobilePhoneNumber() == null || user.getMobilePhoneNumber().isEmpty()) {
                mUserPhone.setText("未绑定");
            } else {
                mUserPhone.setText(user.getMobilePhoneNumber());
            }
            if (user.getEmail() == null || user.getEmail().isEmpty()) {
                mUserEmail.setText("未绑定");
            } else {
                mUserEmail.setText(user.getEmail());
            }
        }
    }

}
